/*
 * Copyright (c) 2010, Thiago Campos Viana.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * Neither the name of Thiago Campos Viana nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


package kernel.gamecode;

import java.nio.ByteBuffer;
import java.util.Random;
/**
 * Character class
 * <p>
 * Base class for Monster, Player, and NPC class
 * @author Thiago Campos Viana
 */
public class Character extends GameObject {

    public static final int bufferSize = 15;
    /**
     * Attack is used to calculate damage
     */
    public int attack;

    
    /**
     * Agility is used to defend, reduce damage, and to determine turn action order
     */
    public int agility;
    /**
     * Armor is used to reduce damage
     */
    public int armor;
    /**
     * Sword is used to increment damage
     */
    public int sword;

    /**
     * Current health points of the character
     */
    public short hp;
    /**
     * Max amount of health points of the character
     */
    public short maxHP;

    /**
     * Current magic points of the character
     */
    public int mp;
    /**
     * Max amount of magic points of the character
     */
    public int maxMP;

    /**
     * The level of the characters, how strong he is
     */
    public short level;
    /**
     * The total experience points
     */
    public int experience;


    /**
     * The total amount of gold
     */
    public int gold;

    /**
     *
     */
    public byte vocation;
    
    /**
     * The name of the character
     */
    public String name;

    /**
     * A random object, used in some calculations
     */
    protected static Random r=new Random(System.currentTimeMillis());

    /**
     * This function is used when a character attacks another character
     * @param character
     * The target character
     * @return
     * Total damage dealed to the attacked character
     */
    public int attack(Character character){
        int total = attack+sword - (character.agility+character.armor);
        if (total < 1) total = 1;
        character.hp-=(total);
        if(character.hp<0)character.hp=0;
        return (total);


    }

    /**
     * Does enemy hitted  the character?
     * @param enemyAgility
     * @return
     * True if character evade, false if character is hitted
     */
    public boolean defend(int enemyAgility){

        //if ( agility < enemyAgility ) return false;

        int range = (agility +  enemyAgility)*2;


        int n = r.nextInt();

        if (n<0) n = -1*n;
        try {
            n = n%range;
        } catch(Exception ie){
            n = 0;
        }

        if(n<agility) return true;
        else return false;


    }

    /**
     * Try scape from battle
     * @param enemyAgility
     * @return
     * True if scaped, false if not
     */
    public boolean scape(int enemyAgility){
        int range = agility +  enemyAgility+2;



        int n = r.nextInt();

        if (n<0) n = -1*n;

        try {
            n = n%range;
        } catch(Exception ie){
            n = 0;
        }

        if(n<=agility) return true;
        else return false;

    }

    /**
     * When dies, character drops gold.
     * <p>
     * Applied only to monsters.
     * @return
     * Total amount of gold dropped
     */
    public int dropGold(){


        int max = gold;
        int min = gold/2;

        int range = max + 1 - min;


        int n = r.nextInt();

        if (n<0) n = -1*n;

        n=n%range;

        gold-=(min+n);

        return (min + n);


    }

    /**
     *
     * @return
     */
    @Override
    public byte[] getBytes() {
        bytes=ByteBuffer.allocate(Character.bufferSize);
        bytes.putInt(id);
        bytes.put(layer);
        bytes.putShort(x);
        bytes.putShort(y);
        bytes.put(direction);

        bytes.put(vocation);
        bytes.putShort(level);
        bytes.putShort(hp);
        //bytes.putShort(MP);


        return bytes.array();
    }

    @Override
    public byte[] update() {
        throw new UnsupportedOperationException("Not supported yet.");
    }



}

